package com.xinxing.city.recovery.adapter;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.ImageFormat;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import com.xinxing.city.recovery.R;

import java.io.IOException;

public class CameraAdapter extends DialogFragment {

    // 在DialogFragment中添加成员变量和setter方法
    private View dialogView;
    private Integer cnum = 0;
    private AlertDialog.Builder builder;
    private SurfaceView m_surfaceView;
    private SurfaceHolder holder;
    private Boolean cameraStatus;
    private Camera camera;
    private Camera.Parameters parameters;

    public CameraAdapter(Integer cn) {
        cnum = cn;
        Bundle args = new Bundle();
        this.setArguments(args);
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // 创建并配置对话框
        builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = LayoutInflater.from(getActivity());
        dialogView = inflater.inflate(R.layout.custom_dialog_video, null);
        builder.setView(dialogView);

        m_surfaceView = dialogView.findViewById(R.id.SuView);


        AlertDialog dialog = builder.create();
        // 设置对话框横向显示
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        initSurface();

        return dialog;
    }

    private void initSurface() {

        holder = m_surfaceView.getHolder();
        holder.setFormat(PixelFormat.RGBA_8888);
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                videoStart();
            }
            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }
            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

            }
        });
    }

    //监控
    public void videoStart() {
        cameraStatus = false;
        try {
            if (!cameraStatus) {
                camera = Camera.open(cnum);
                startCamera(0, holder);
                cameraStatus = true;
            } else {
                stopCamera(cnum);
                cameraStatus = false;
            }
        } catch (Exception e) {
            camera = null;
            e.printStackTrace();
        }
    }

    private void startCamera(int cameraId, SurfaceHolder holder) {

        if (camera != null) {
            try {
                parameters = camera.getParameters();
                parameters.setPictureFormat(ImageFormat.JPEG);
                parameters.setPreviewSize(1920, 1080);
                parameters.setPictureSize(1920, 1080);
                parameters.setPreviewFrameRate(30);
                camera.setParameters(parameters);
                camera.setPreviewDisplay(holder);
                camera.startPreview();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void stopCamera(int cameraId) {
        if (camera != null) {
            camera.stopPreview();
            camera.release();
            camera = null;
        }
    }
}
